define(function(require, exports, module) {
  var words = {}; //生词表
  var tool = require('tool');
  var $list = null;
  
  function wordstring(word){
    return "<div word='"+word.word+"' class='item'><span>"+word.count+"</span> <span class='word'>"+word.word+"</span></div>  "
  }
  
  function storeWord(key,value){
      words[key]=value;
      localStorage.setItem( "words",JSON.stringify(words) );
  }
  
  function read(){
    tool.readWord($(this).attr('word'));
  }

  exports.update = function(word){
    var count = 1;
    if(words[word]){
      count = words[word].count+1;
    }
    wordobject = {time:new Date().getTime(),count:count,word:word}
    storeWord(word,wordobject);
    
    if( $list.find("[word='"+word+"']").length==0){
      $list.prepend( wordstring(wordobject) );
    }
  }
  
  exports.show = function ($sc){
    //生词表
    $list = $sc;
    $("[word]").live('click',read);
    if( localStorage.getItem("words")!=null ){
      words = JSON.parse(localStorage.getItem("words"));
    }
    wordArray = []
    for(x in words){
      wordArray.push(words[x])
      wordArray.sort(function(a,b){return b.time-a.time});
    }
    if(wordArray.length==0)
      return;
    
    for(x in wordArray){
      if(x>50){
          delete words[wordArray[x]];
      }else{
        $list.append( wordstring(wordArray[x]) );
      }
    }
  }
});